(function(app){
	app
	.controller("content_50_inputCtrl",["$scope","$state","myFactory","$http","$stateParams","$state","$q","$ionicHistory",function($scope,$state,myFactory,$http,$stateParams,$state,$q,$ionicHistory){
		
		//接收参数
		var itemId = $stateParams.changeTypeId;//变更项目ID
		var chdrnum01 =  $stateParams.chdrnum01; //保单号
		var chdrcoy = $stateParams.chdrcoy; //保单公司号码
		var searchParam = $stateParams.searchParam;
		var policyData;//保单详情
		var itemData;//用于接收初始化查询initContentItem的内容，里面存在各种各样的内容
		var initFlag = false;//检测标志，在初始化函数完成后，会被置为true
		
		$scope.ReceiveType=null;
		//绑定参数到页面
		$scope.chdrnum01 = $stateParams.chdrnum01;
	//	$scope.readFlag = false;//阅读协议标志，默认为false
		var token=sessionStorage.getItem("token");
/***************************************************************初始化部分begin****************************************************/
		
		//初始化查询变更项内容
		$scope.load = function () {
			myFactory.loadingShow();//显示加载圈
			var timestamp=myFactory.getTimetamp();
			var sfis= "itemId-token-timestamp";
			var signStr="itemId="+itemId+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
			var sign = myFactory.hex(signStr);
			$http({
				method: 'GET',
				url: app.domain + 'input/initContentItem.action',
				params: {'itemId': itemId,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
			}).then(function successCallback(response) {
				if (response.data.success) {
					itemData = response.data.data.modelMap;
					$scope.item50 = itemData.item50;
					console.log($scope.item50);
					//如果是第一次做变更，即没有缓存的时候，根据原先已有的方式，自动选择另外一种方式
					if( null == $scope.item50.receiveType || "" == $scope.item50.receiveType ){	
						if($scope.item50.receiveTypeOld =="12"){//当原先方式是12，也就是月领的时候，改变成年领
							$("#ReceiveType").val("01");
							$scope.ReceiveTypeOptionSelect = "01";
						}else{//否则默认月领
							$("#ReceiveType").val("12");
							$scope.ReceiveTypeOptionSelect = "12";
						}
					}else{//存在缓存的时候，直接采取缓存
						$("#ReceiveType").val($scope.item50.receiveType);
					}
					$("#ReceiveTypeName").val($("#ReceiveType option:selected").text());
					initFlag=true;//标识初始化成功
				}
				myFactory.loadingHide();
			}, function errorCallback(response) {
				myFactory.loadingHide();
				myFactory.alert("","初始化查询变更项内容失败!","info");
			});		
		}
		
/***************************************************************初始化部分end****************************************************/		
		
			$scope.data = {
				//选择新的红利领取方式
/*				chooseWlfare: [
					{
						title: '月领',
						id: 12
					},
					{
						title: '年领',
						id: 01
					}					
				]*/
			},
									
			//重新受理
			/*$scope.renew = function(){
				$.confirm("是否重新受理？", "", function() {
					console.info("重新受理");
					myFactory.renew();
				}, function() {
					console.info("取消重新受理");
				});
			}	*/
			$scope.renew = function(){
				myFactory.renew();
			}
			//退出移动保全
			$scope.exit = function(){
				myFactory.exit();
			}
												
			//下一步
			$scope.nextStep = function(){
				//判断是否初始化完成了，如果失败，不予进入下一步
				if(!initFlag){
					myFactory.alert("","初始化时失败，请返回上一步重试","info");
					return;
				}
				
				var oldReceiveType  = $("#oldReceiveType").val();
				var ReceiveType  = $("#ReceiveType").val();
				if( ReceiveType == '' || oldReceiveType == ReceiveType ){
					myFactory.alert('','年金领取方式没有改变，请选择其他领取方式！','info');
					return false;
				}
				var item = $scope.item50;
				$.extend(item,{"receiveType":ReceiveType,"receiveTypeName":$("#ReceiveTypeName").val()});
				$ionicHistory.clearCache().then(function(){
					$state.go('content_check_main',{
						itemId:itemId,
						item:item,
						chdrnum01:chdrnum01,
						chdrcoy:chdrcoy
					});
				})
			}
			
			
			
			
			//返回上一页
			$scope.goback = function(){
				myFactory.goBack();
			}
			
			//保单详情弹框
			$scope.showPolicyDetail = function(){
				modal('open','policyModalOn50Input','overwindowOn50Input','policyCloseXOn50Input');
				var timestamp=myFactory.getTimetamp();
				var sfis= "chdrnum-chdrcoy-token-timestamp";
				var signStr="chdrnum="+chdrnum01+"&chdrcoy="+chdrcoy+"&token="+token+"&timestamp="+timestamp;//encodeURI(name)
				var sign = myFactory.hex(signStr);
				//初始化时候查询保单详情
				$http({
					method: 'GET',
					url: app.domain + 'query/restFindPolicyDetail.action',
					params: {'chdrnum': chdrnum01,'chdrcoy': chdrcoy,'sign':sign,'sfis':sfis,'token':token,'timestamp':timestamp}
				}).then(function successCallback(response) {
					if (response.data.success) {
						policyData = response.data.data;
						$scope.policyData = policyData;//绑定至页面
						$scope.policyData.ptdate01=myFactory.DateTools($scope.policyData.ptdate01);
					}
				}, function errorCallback(response) {
					myFactory.alert("","初始化查询订单详情失败!","info");
				});
			}
			//保单详情弹窗关闭函数
			$scope.policyCancelOn50Input = function(){
				modal('close','policyModalOn50Input','overwindowOn50Input','policyCloseXOn50nput');
			}
			
			//关闭遮罩
			$scope.overwindowClose = function(){
				//$(".overwindow").show();
				$(".overwindow").hide();
				$(".overmodal").hide();
			}
			
			$scope.receiveTypeOptionChange = function(){
				var options = "";
				options =$("#ReceiveType option:selected");
				$scope.ReceiveType=options.text();
				console.log("test1:"+options.val());
				console.log("test2:"+options.text());
				$("#ReceiveTypeName").val(options.text());
			}							
		}]);
	
	//指针弹窗
	app.directive('infoid',function(){
		return {
			restrict: 'EA',		//指明指令在DOM里面以什么形式被声明,EA.表示即可以是元素也可以是属性
			templateUrl: 'html/input/content_base_info.html'+app.clearCache,
			replace: true,		//设置解析后，DOM声明的指令是否消失
			link: function(scope,element,attrs){  //链接函数负责注册DOM事件和更新DOM,
				console.log('指令完成');
			}
		}
	});	
	
})(app)